package com.wennimei.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wennimei.domain.HistoryToday;
import com.wennimei.domain.SinaNews;
import com.wennimei.domain.baidu.Place;
import com.wennimei.enums.MessageTypeEnum;
import com.wennimei.util.DataUtil;

@Service
public class ProcessService {
    @Autowired
    private BaiduDituService baiduDituService;

    @Autowired
    private HistoryTodayService historyTodayService;

    @Autowired
    private WeatherService weatherService;

    @Autowired
    private DianpingService dianpingService;

    @Autowired
    private YoudaoTranslateService youdaoTranslateService;

    @Autowired
    private MobileBelongService mobileBelongService;

    @Autowired
    private StockService stockService;

    @Autowired
    private ExpressService expressService;

    @Autowired
    private AirService airService;

    @Autowired
    private JokeService jokeService;

    @Autowired
    private ChinaCodeService chinaCodeService;

    @Autowired
    private CityTourService cityTourService;

    @Autowired
    private StarMatchService starMatchService;

    @Autowired
    private BaiduBaikeService baiduBaikeService;

    @Autowired
    private PictureSearchService pictureSearchService;

    @Autowired
    private ParameterService parameterService;

    @Autowired
    private SoftService softService;

    @Autowired
    private MusicService musicService;

    @Autowired
    private NewsService newsService;

    @Autowired
    private HoroscopeService horoscopeService;

    @Autowired
    private AAService aaService;

    @Autowired
    private RailwayService railwayService;

    public Object getContentResult(MessageTypeEnum type, String content) {
        return this.getContentResult(type, content, null, null, null);
    }

    public Object getContentResult(MessageTypeEnum type, String content,
            String lat, String lon, String user) {
        StringBuilder sb = new StringBuilder();
        if (type.equals(MessageTypeEnum.HELP))
            return sb.append(DataUtil.WELCOME_CONTENT).toString();
        else if (type.equals(MessageTypeEnum.WELCOME))
            return sb.append(DataUtil.WELCOME_CONTENT).toString();
        else if (type.equals(MessageTypeEnum.BAIDU_PLACE)) {
            List<Place> list = this.baiduDituService.getPlaces(content, null,
                    null, "3000", "北京");
            if (null == list || list.isEmpty())
                return null;
            int i = 1;
            for (Place p: list) {
                sb.append(i + "、").append(p.getName()).append("：")
                        .append(p.getAddress()).append("；\r\n");
                i++;
            }
            return sb.toString();
        } else if (type.equals(MessageTypeEnum.HISTORY_TODAY)) {
            List<HistoryToday> list = this.historyTodayService
                    .get(new SimpleDateFormat("MMdd").format(new Date()));

            if (null == list || list.isEmpty())
                return null;
            int i = 1;
            for (HistoryToday h: list) {
                sb.append(i + "：").append(h.getDate()).append(",")
                        .append(h.getTitle()).append(";\r\n");
                i++;
            }
            return sb.toString();
        } else if (type.equals(MessageTypeEnum.WEATHER)) {
            content = content.replace("天气", "");
            content = content.replace("的", "");
            content = content.replace("省", "");
            content = content.replace("市", "");
            content = content.replace("区", "");
            return this.weatherService.get(content);

        } else if (type.equals(MessageTypeEnum.DIANPING))
            return this.dianpingService.findBusiness(lat, lon);
        else if (type.equals(MessageTypeEnum.TRANSLATE))
            return this.youdaoTranslateService.get(content);
        else if (type.equals(MessageTypeEnum.MOBILE_BELONG))
            return this.mobileBelongService.get(content);
        else if (type.equals(MessageTypeEnum.STOCK))
            return this.stockService.get(content);
        else if (type.equals(MessageTypeEnum.EXPRESS))
            return this.expressService.get(content);
        else if (type.equals(MessageTypeEnum.AIR_STATE))
            return this.airService.getAirState(content);
        else if (type.equals(MessageTypeEnum.JOKE))
            return this.jokeService.get();
        else if (type.equals(MessageTypeEnum.CHINA_CODE))
            return this.chinaCodeService.get(content);
        else if (type.equals(MessageTypeEnum.STAR_MATCH))
            return this.starMatchService.get(content);
        else if (type.equals(MessageTypeEnum.CITY_TOUR))
            return this.cityTourService.get(content);
        else if (type.equals(MessageTypeEnum.BAIDU_BAIKE))
            return this.baiduBaikeService.get(content);
        else if (type.equals(MessageTypeEnum.PICTURE_SEARCH))
            return this.pictureSearchService.get(content);
        else if (type.equals(MessageTypeEnum.PARAMETER))
            return this.parameterService.get(content);
        else if (type.equals(MessageTypeEnum.SOFT_DOWN))
            return this.softService.get(content);
        else if (type.equals(MessageTypeEnum.MUSIC))
            return this.musicService.get(content);
        else if (type.equals(MessageTypeEnum.NEWS)) {
            List<SinaNews> list = this.newsService.get(content);
            int i = 1;
            for (SinaNews s: list) {
                sb.append("[" + i + "]" + s.getTitle().trim()).append(":")
                        .append("(" + s.getPubDate() + ")")
                        .append(s.getDescription().trim()).append(".详情:")
                        .append(s.getLink()).append("\r\n");
                i++;
            }
            return sb.toString();
        } else if (type.equals(MessageTypeEnum.HOROSCOPE))
            return this.horoscopeService.get(content);
        else if (type.equals(MessageTypeEnum.CHAT))
            return this.aaService.getAnswer(content);
        else if (type.equals(MessageTypeEnum.RAILWAY))
            return this.railwayService.get(content);

        return null;
    }
}
